<?php
namespace Home\Controller;
use Think\Controller;

class UploadController extends Controller {

    public function index(){

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     10240000;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            echo json_encode(['file' => '']);
        }else{// 上传成功
            foreach($info as $file){
                echo json_encode(['file' => $file['savepath'].$file['savename']]);
            }
        }
        exit();
    }

    //上传身份证 接口
    public function index_new(){

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     10840000 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','bmp','gig','tif','rgb','dib','eps','jpe','jpe','pcx');// 设置附件上传类型


        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            //echo json_encode(['file' => 0, 'msg' => $upload->getError()]);
           echo json_encode(['file' => '']);
        }else{// 上传成功
            $img[] = $this->thumb($info, 500, 500, 'l_');
            echo json_encode(['file' => $img[0]]);
        }
        exit();
    }

    //生成缩略图
    public function thumb($info, $w, $h, $type = 's_'){

        foreach($info as $file){

            $file_url =  './Uploads/'. $file['savepath'].$file['savename'];
            $file_thumb =  './Uploads/'. $file['savepath'].$type.$file['savename'];

            $image = new \Think\Image();
            $image->open($file_url);
            // 按照原图的比例生成一个最大为750*500的缩略图并保存为thumb.jpg
            $image->thumb($w, $h, \Think\Image::IMAGE_THUMB_SCALE)->save($file_thumb);

            return $file['savepath'].$type.$file['savename'];
        }
    }
}